import * as Sequelize from 'sequelize'
import { CHAR, ENUM, FLOAT, INTEGER, STRING } from 'sequelize'

const force = true

export const sequelize = new Sequelize('sequelize_demo', 'misty', null, {
  host: 'localhost',
  dialect: 'mysql',
  timezone: '+08:00',
  dialectOptions: {
    passwordSha1: Buffer.from('36bdbce7890bcce2bb4cda4d2eb29825f6befde8', 'hex'),
  },
  pool: {
    max: 5,
    min: 0,
    idle: 10000
  }
})

/*
 sequelize.authenticate().then(() => {
 console.log('Connection has been established successfully.')
 }).catch(err => {
 console.error('Unable to connect to the database:', err)
 })

 const User = sequelize.define('user', {
 firstName: {
 type: Sequelize.STRING,
 },
 lastName: {
 type: Sequelize.STRING,
 },
 birthday: {
 type: Sequelize.DATE,
 get(){
 const time = this.getDataValue('birthday')
 return Date.parse(time)
 }
 }
 }, { tableName: 'user' })

 User.sync({ force }).then(() => {
 return User.create({
 firstName: 'John',
 lastName: 'Hancock',
 birthday: '1988-9-7 00:00:00'
 })
 }).then(() => {
 // User.findAll().then(users => {
 //   console.log(users)
 // })
 }).then(() => {
 User.findOne().then((user: any) => {
 console.log(JSON.stringify(user))
 })
 })
 */

const Project = sequelize.define('project', {
  title: Sequelize.STRING,
  description: Sequelize.STRING,
}, {
  indexes: [
    { type: 'FULLTEXT', fields: [ 'title', 'description' ], parser: 'ngram' } as any
  ]
})
Project.sync({ force })

/*
 const Task = sequelize.define('task', {
 title: Sequelize.STRING,
 description: Sequelize.TEXT('500'),
 deadline: Sequelize.DATE,
 })
 */

/*
 Task.sync({ force }).then(() => {
 return Task.create({
 title: 'Running',
 description: 'On the way',
 deadline: '2017-08-01 00:00:00'
 })
 })
 */

/*
 const Bar = sequelize.define('bar', {})

 const Foo = sequelize.define('foo', {
 flag: { type: Sequelize.BOOLEAN, allowNull: false, defaultValue: true },
 myDate: { type: Sequelize.DATE, defaultValue: Sequelize.NOW },
 title: { type: Sequelize.STRING, allowNull: false },

 uniqueOne: { type: Sequelize.STRING, unique: 'compositeIndex' },
 uniqueTwo: { type: Sequelize.INTEGER, unique: 'compositeIndex' },

 someUnique: { type: Sequelize.STRING, unique: true },

 identifier: { type: Sequelize.STRING, primaryKey: true },

 incrementMe: { type: Sequelize.INTEGER, autoIncrement: true },

 fieldWithUnderscores: { type: Sequelize.STRING, field: 'field_with_underscores' },

 bar_id: {
 type: Sequelize.INTEGER,
 references: {
 model: Bar,
 key: 'id',
 }
 }
 })
 */

// Bar.sync({ force })
// Foo.sync({ force })

export const CityJS = sequelize.define('city', {
  id: { type: INTEGER, primaryKey: true, autoIncrement: true },

  name: { type: STRING },

  countryCode: { type: STRING },

  district: { type: STRING },

  population: { type: INTEGER },
}, { tableName: 'city', timestamps: false })

export const CountryJS = sequelize.define('country', {
  code: { type: CHAR(3), primaryKey: true, autoIncrement: true },

  name: { type: CHAR(52) },

  continent: { type: ENUM('Asia', 'Europe', 'North America', 'Africa', 'Oceania', 'Antarctica', 'South America') },

  region: { type: CHAR(26) },

  surfaceArea: { type: FLOAT },

  indepYear: { type: INTEGER(6) },

  population: { type: INTEGER },
}, { tableName: 'country', timestamps: false })

CountryJS.hasMany(CityJS)
CityJS.belongsTo(CountryJS)
